<?php
// $Id$

include_once('BaseModel.class.php');

class PollModel extends BaseModel
{
    function collect($offset=null,$limit=null,$search=array(),$by='1',$sort='',$u_id='',$u_prv='')
    {
        $where = ($search) ? "WHERE LOWER({$search['searchfield']}) LIKE '%{$search['searchtext']}%'" : "WHERE 1";

        if (is_null($offset)) {
            
                  if ($u_prv="ADMINISTOR" OR $u_prv="SYSTEM ADMINISTRATOR"){
                $query = "SELECT COUNT(*) FROM polls $where AND active=1";
                  }else{
                $query = "SELECT COUNT(*) FROM polls $where AND auth_id ='$u_id' AND active=1";
                  }
        } else {
                  if ($u_prv="ADMINISTRATOR" OR $u_prv="SYSTEM ADMINISTRATOR"){
                    $query = "SELECT * FROM polls $where AND active=1 ORDER BY $by $sort LIMIT $offset,$limit";
                  }else{
                    $query = "SELECT * FROM polls $where AND auth_id='$u_id' AND active=1 ORDER BY $by $sort LIMIT $offset,$limit";
                  }
        }
        return $this->query($query, true);
    }

    function detail ($id)
    {
        $query = "SELECT * FROM polls WHERE id='$id'";
        return $this->query($query, true);
    }

    function options ($id)
    {
        $query = "SELECT poll_options.*, COUNT(pa.id) as votes FROM poll_options
                  LEFT JOIN poll_answers pa ON pa.poll_option_id = poll_options.id 
                  WHERE poll_options.poll_id='$id' AND poll_options.active=1
                  GROUP BY poll_options.id";
        return $this->select_all($query);
    }


    /*
     * Add members and return the new member_id
     *
     */
    function ins($detail)
    {
        return $this->insert('polls', $detail);
        return $this->last_insert_id('polls');
    }

    /*
     * Update categories for a particular categories_id
     *
     */
    function edit ($id, $detail)
    {
        return $this->update('polls', $detail, "id = '$id'");
    }

    /*
     * Delete members detail for a particular member_id
     *
     */
    function deactivate ($id)
    {
        $query = "UPDATE polls SET active = '0' WHERE id='$id'";
        return $this->query($query, false);
    }
    function activate ($id)
    {
        $query = "UPDATE polls SET active = '1' WHERE id='$id'";
        return $this->query($query, false);
    }
    function get_all(){
        $sql = "SELECT * from polls where active='1'";
        return $this->select_all($sql);
        
    }
    function get_result($pid){
        $sql = "SELECT COUNT(distinct msisdn) as distinct_vote,COUNT(*) as total_sms, po.*FROM `poll_answers` pa
        JOIN poll_options po ON po.id=pa.`poll_option_id`  
        JOIN polls p ON po.poll_id=p.id
        WHERE p.id='$pid'
        group by po.id";
        return $this->select_all($sql);
    }
    function voter_list($id){
        $sql = "SELECT pa.*, CONCAT(m.firstname, ' ',m.lastname) as name FROM `poll_answers` pa
        LEFT JOIN members m on m.msisdn=pa.msisdn 
        WHERE pa.poll_option_id='$id'";
        return $this->select_all($sql);
    }
}

?>
